package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.po.User;
import com.zjitc.service.UserService;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.Request;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 13:53
 * @description:
 */

@Controller
@RequestMapping(value = "/user",method = RequestMethod.POST)
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/test")
  @ResponseBody
  public ServerResponse test() {
    User user = new User();
    user.setId(1);
    user.setUsername("hh");
    user.setPassword("pishf");
    user.setEmail("aa@mail.com");
    user.setQuestion("hello");
    user.setAnswer("word");
    user.setRole(0);
    user.setCreate_time(new Date());
    user.setUpdate_time(new Date());
    return ServerResponse.createSucess(user);
  }

  //2.4.1 登录
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse login(@RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password, HttpSession session) {
    ServerResponse<User> response = userService.login(username, password);
    if (response.success()) {
      session.setAttribute("user", response.getData());
    }
    return response;
  }

  //2.4.2 注册
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse register(User user) {
    ServerResponse<User> response = userService.register(user);
    return response;
  }

  //2.4.3 检查用户名是否有效
  @RequestMapping(value = "/check_valid", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse check_valid(@RequestParam(name = "str") String str,
      @RequestParam(name = "type") String type) {
    ServerResponse<User> response = userService.check_valid(str, type);
    return response;
  }

  //2.4.4 获取登录用户信息
  @RequestMapping(value = "get_user_info", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse get_user_info(HttpSession session) {
    User userSession = (User) session.getAttribute("user");
    if (userSession == null) {
      return ServerResponse.createError("用户还暂未登录");
    }
    ServerResponse<User> response = userService.get_user_info(userSession.getId());
    return response;
  }

  //2.4.5 忘记密码
  @RequestMapping(value = "forget_get_question", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse forget_get_question(@RequestParam(name = "username") String username) {
    ServerResponse<User> response = userService.forget_get_question(username);
    return response;
  }

  //2.4.6 提交问题答案
  @RequestMapping(value = "forget_check_answer",method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse forget_check_answer(@RequestParam(name = "username") String username,
      @RequestParam(name = "answer")String answer,@RequestParam(name = "question") String question) {
    ServerResponse<User> response =userService.forget_check_answer(username,question,answer);
    return response;
  }
  //2.4.7 忘记密码的重设密码
  @RequestMapping(value = "forget_reset_password",method = RequestMethod.GET)
  @ResponseBody
  public ServerResponse forget_reset_password(@RequestParam(name = "username")String username, @RequestParam(name = "passwordNew")String passwordNew, @RequestParam(name = "forgetToken")String forgetToken){
    ServerResponse<User> response =userService.forget_reset_password(username,passwordNew,forgetToken);
    return response;
  }
  //2.4.8 登录中状态重置密码
  @RequestMapping(value = "reset_password", method = RequestMethod.GET)
  @ResponseBody
  public ServerResponse reset_password(@RequestParam(name = "passwordOld") String passwordOld,
      @RequestParam(name = "passwordNew") String passwordNew, HttpSession session) {
    User userSession = (User) session.getAttribute("user");
    if (userSession == null) {
      return ServerResponse.createError("用户还暂未登录");
    }
    ServerResponse<User> response = userService
        .reset_password(userSession.getId(), passwordOld, passwordNew);
    return response;
  }

  //2.4.9 登录状态更新个人信息
  @RequestMapping(value = "update_information", method = RequestMethod.GET)
  @ResponseBody
  public ServerResponse update_information(User user, HttpSession session) {
    User userSession = (User) session.getAttribute("user");
    if (userSession == null) {
      return ServerResponse.createError("用户还暂未登录");

    }
    ServerResponse<User> response = userService.update_information(user, userSession.getId());
    return response;
  }

  //2.4.10 获取当前登录用户的详细信息，并强制登录
  @RequestMapping(value = "get_information", method = RequestMethod.GET)
  @ResponseBody
  public ServerResponse get_information(HttpSession session) {
    User userSession = (User) session.getAttribute("user");
    if (userSession == null) {
      return ServerResponse.createError("用户还暂未登录");
    }
    ServerResponse<User> response = userService.get_information(userSession.getId());
    return response;
  }

  //2.4.11 退出登录
  @RequestMapping(value = "logout", method = RequestMethod.GET)
  @ResponseBody
  public ServerResponse logout(HttpSession session) {
    User userSession = (User) session.getAttribute("user");
    if (userSession == null) {
      return ServerResponse.createError("服务端异常");
    }
    session.removeAttribute("user");
    return ServerResponse.createSucess("退出成功");
  }
}